# # 拓展小知识点
# print(abs(5))  # 计算某个数的绝对值
# print(abs(-5))  # 计算某个数的绝对值
# # 计算四舍五入后的值
# print(round(1.4))
# print(round(1.6))
# print(round(1.5))
# print(round(2.5))
# print(round(3.5))
# print(round(4.5))
#
# # 计算两个数绝对值的和
# # 常规做法:
# def fun(x,y):
#     res = abs(x)+abs(y)
#     return res
#
#
# print(fun(4, -9))
# # 高阶函数实现:
# def fun2(x,y,f):
#     res = f(x)+f(y)
#     return res
#
#
# print(fun2(4, -9, abs))
#
# # 计算两个数四舍五入后的和
# print(fun2(1.6, 2.3, round))
# ==========================================================
# 内置高阶函数
# 需求:计算list1序列中各个数字的2次方。
#map讲解
list1 = [1,2,3,4,5]
def fun(n):
    return n**2

res = map(fun,list1)
print(list(res))
# -----------------------------------------
# reduce函数
import functools
# 需求: 计算list1序列中各个数字的累加和。
list1 = [1, 2, 3, 4, 5]

def fun(x, y):
    return x + y

res = functools.reduce(fun, list1)
print(res)

# -----------------------
# filter过滤
# 找出列表内的偶数
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
def fun(x):
    res = x % 2 == 0
    return res
for i in list1:
    print(f"当前元素:{i}结果:{fun(i)}")

res2 = filter(fun, list1)
print(list(res2))
